package com.xiaomaoguai.meizitu;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @Description :
 * @FileName: PaserURL.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年6月6日 下午2:39:52
 * @Version:V1.00
 */
public class PaserURL3 {

	private static final String URL = "http://www.mnsfz.com";
	private static String SAVE_PATH = "D://meinv/";
	private static long num = 0L;
	private final static ExecutorService es = Executors.newFixedThreadPool(10);
	private static final HttpClient httpClient;
	private final static String CONNECTION = "keep-alive";
	private final static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36";

	static {
		RequestConfig config = RequestConfig.custom().setConnectTimeout(1000).setSocketTimeout(1000).build();
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(config);
		httpClient = httpClientBuilder.build();
	}

	public static void main(String[] args) throws IOException {
		Connection conn = getConnection(URL);
		getUrl(conn.get());
		//parser("http://www.mnsfz.com/h/qingchun/PmamdbbdbJdaiHmPe.html");
	}

	public static void parser(String pageUrl) throws IOException {
		Connection conn = getConnection(pageUrl);
		Document doc = conn.get();
		Elements pageElements = doc.getElementsByClass("pageList");
		int size = pageElements.get(0).childNodeSize();
		int indexOf = pageUrl.lastIndexOf(".");
		String lastPage = pageUrl.substring(0, indexOf) + "-" + (size - 2) + ".html";
		System.out.println(lastPage);
		getPicUrl(lastPage);
	}

	public static void getPicUrl(String lastPage) throws IOException {
		Connection conn = getConnection(lastPage);
		Document doc = conn.get();
		Element parent = doc.getElementById("b-show");
		Elements elements = parent.getElementsByTag("script");
		Element element = elements.get(4);
		String text = element.html();
		String[] strings = text.split(";");
		for (int i = 1; i < strings.length - 1; i++) {
			String substring = strings[i].substring(13, strings[i].length() - 1);
			String replaceAll = substring.replaceAll("big", "pic");
			downImg(replaceAll, ".jpg");
		}
	}

	public static void getUrl(Document doc) throws IOException {
		Elements ulElements = doc.getElementsByClass("pic");
		for (Element element : ulElements) {
			Elements aElements = element.getElementsByTag("a");
			for (Element element2 : aElements) {
				String href = element2.attr("href");
				parser(URL + href);
			}
		}
	}

	public static void doTask(String httpUrl, String suffix) {
		es.execute(new Runner(httpUrl, suffix));
	}

	public static void downImg(String httpUrl, String suffix) throws ClientProtocolException, IOException {
		HttpGet httpGet = new HttpGet(httpUrl);
		httpGet.setHeader(HttpHeaders.USER_AGENT, USER_AGENT);
		HttpResponse response = httpClient.execute(httpGet);
		int statusCode = response.getStatusLine().getStatusCode();
		if (statusCode != 200) {
			httpGet.abort();
			throw new RuntimeException("HttpClient,error status code :" + statusCode);
		}
		HttpEntity entity = response.getEntity();
		InputStream inputStream = entity.getContent();
		File file = new File(SAVE_PATH + (num++) + suffix);
		FileUtils.copyInputStreamToFile(inputStream, file);
	}

	private static class Runner implements Runnable {

		private String url;

		private String fileName;

		public Runner(String url, String fileName) {
			super();
			this.url = url;
			this.fileName = fileName;
		}

		public void run() {
			try {
				downImg(url, fileName);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static Connection getConnection(String url) {
		Connection con = Jsoup.connect(url);
		con.header(HttpHeaders.CONNECTION, CONNECTION);
		con.header(HttpHeaders.USER_AGENT, USER_AGENT);
		return con;
	}

}
